package cn.itcast.web04.response;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 重定向通过登录案例来实现
 * 需求分析：
 * 1、在登录页面输入用户名和密码
 * 2、在LoginServlet里面判断用户名和密码是否正确，
 * 如果输入错误，会重新定向到登录页面；如果正确，跳转到欢迎页面
 * 
 * 获取登录页面提交的用户名和密码：request.getParameter()方法
 * 
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public LoginServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("doGet运行了");
	    // 1、获取登录的用户名和密码
	    String name = request.getParameter("username");
	    String password = request.getParameter("psw");
	    // 2、判断用户名和密码是否正确
	    if (name.equals("张三") && password.equals("123")) {
	        // 表明输入的数据是正确的
	        System.out.println("正确");
	        response.sendRedirect("/web_04/welcome.html");
	    } else {
	        System.out.println("错误");
	        response.sendRedirect("/web_04/login.html");
	    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
